<template>
	<view class="login">
		<image src="/static/logo_login@2x.png" mode="widthFix" style="width: 200rpx;margin-bottom: 60rpx;"></image>
			<view class="title">
				肥猫商城
			</view>
			<view class="button"><button type="warn" open-type="getUserInfo" @getuserinfo="getuserinfo">微信授权登录</button></view>
	</view>
</template>

<script>
import md5 from '@/common/api/md5.js';
import { passwordLogin } from '@/common/api/auth.js';
import { mapState, mapActions } from 'vuex';
import { requestUrl } from '@/common/config.js';
export default {
	name: 'login',
	data() {
		return {
			phone: '13023779780',
			codeNum: '',
			referrerPhone: null,
			res_user_no: null,
			code1: 1,
			access_token: '',
			Code: '',
			time: 60
		};
	},

	computed: {
		...mapState(['token'])
	},
	onLoad: function() {
		let _this = this;
		// 查看是否授权
		uni.login({
			success: function(res) {
				_this.Code = res.code;
				// 获取用户信息
			}
		});
	},
	mounted() {
		if (this.token) {
			this.$redirectTo('@/home/home/home');
		}
		uni.getStorage({
			key: 'REF_USER_NO',
			success: res => {
				this.res_user_no = res.data;
			}
		});
	},
	created() {},
	methods: {
		getuserinfo() {
			let _this = this;
			let time = Math.round(new Date().getTime() / 1000).toString();
			let Time = ['time', time].join('=');
			let Access_token = ['access_token', this.access_token].join('=');
			let content = ['content=pinet.FeiMao.2017'].join();
			let arr1 = [content, Time].sort(function(a, b) {
				a - b;
			});
			let newarr = md5(arr1.join('&'));
			uni.login({
				scopes: 'auth_user',
				success: function(loginRes) {
					console.log(loginRes,'loginRes')
					uni.getUserInfo({
						success: function(res) {
							console.log(res,'getuserinfo')
							_this.$request('/app/user/smallAppLogin','post',{
								oauth: 'weixin',
								code: _this.Code,
								encryptedData: res.encryptedData,
								iv: res.iv,
							}).then(res=>{
								console.log(res,111111);
								uni.setStorage({
									key: 'access_token',
									data: JSON.stringify(res.content.user_info.access_token),
									success: function() {}
								});
								uni.setStorage({
									key: 'customer_id',
									data: JSON.stringify(res.content.user_info.customer_id),
									success: function() {}
								});
								uni.setStorage({
									key: 'user_info',
									data: JSON.stringify(res.content.user_info),
									success: function() {}
								});
								uni.showToast({
									title: res.message,
									icon: 'none'
								});
								if (res.content.is_bind) {
									uni.navigateBack({
										delta:1
									})
								}else{
									uni.redirectTo({
										url: '/pages/login/register/register?id=1'
									});
								}
							})
						
						}
					});
				}
			});
		},

	}
};
</script>

<style lang="less" scoped>
.login {
	height: 100%;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	.title{
		font-size: 36rpx;
		font-family: PingFangSC-Semibold, PingFang SC;
		font-weight: 600;
		color: #111111;
		margin-bottom: 60rpx;
	}
		.button {
			width: 90%;
			    margin-bottom: 240px;
			button {
				border-radius: 20px;
			}
		}
	}
</style>
